<?php

namespace app\api\controller;

use app\api\basic\BaseController;
use app\cassification\logic\ClassificationDictionaryLogic;
use app\cassification\model\ClassificationDictionary;
use support\Request;
use app\finance\logic\financeLogic;
use app\financeInstitution\logic\financeInstitutionLogic;
use app\api\annotation\LoginRequired;

class FinanceController extends BaseController {
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();

        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
    //找融资列表
    #[LoginRequired(required:false)]
    public function Lists(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['limit',10],
            ['page',1],
            ['institution_id',''],
            ['finance_category',''],
            ['keyword','']
           
        ]);
        $financeLogic=new  financeLogic();
      
        $financeData=$financeLogic->getFinanceLists($param['lang_code'],
        $param['keyword'],$param['institution_id'],$param['finance_category'],$this->userId,$param['limit']);

       
        return $this->success($financeData);
    }
    /**
     * 找融资详情
     */
    #[LoginRequired(required: false)]
    public function details(Request $request){
        $param = $request->more([
            ['finance_id',''],
        ]);
         $financeLogic=new  financeLogic();
        if(!$param['finance_id']) return $this->fail('缺少产品id');
        $activityData=$financeLogic->getFinanceDetail($param['finance_id'],$this->userId);

        if($activityData) return $this->success($activityData);
       
        return $this->fail('数据不存在');
    }
    /**
     * 获取机构信息
     */
    #[LoginRequired(required:false)]
     public function institutionLists(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['limit',10],
            ['page',1]
           
        ]);
           ;
        $logic = new ClassificationDictionaryLogic();
        $result = $logic
            ->where('classification_type', 5)
            ->field('dictionary_id as value,classification_name_zh as label')
            ->order('dictionary_id desc')  // 增加排序保证结果一致性
            ->select()
            ->toArray();
       // print_r($result);
        $financeInstitutionLogic=new  financeInstitutionLogic();

        foreach ($result as $key => &$value){
            $financeData=$financeInstitutionLogic->where([
                'lang_code'=>$param['lang_code'], 'finance_category' =>$value['value']
            ])->field('institution_id,institution_name,institution_logo')
                ->select();
            $value['financeData'] = $financeData;
        }

        return $this->success(['data'=>$result]);
    }
     /**
     * 获取机构详情
     */
    #[LoginRequired(required:false)]
     public function institutionDetails(Request $request){
        $param = $request->more([
            ['institution_id',''],
           
        ]);
        $financeInstitutionLogic=new  financeInstitutionLogic();
        if(!$param['institution_id']) return $this->fail('缺少机构id');
      
        $financeData=$financeInstitutionLogic->search([
            'institution_id'=>$param['institution_id'],
        ])->field('institution_id,institution_name,institution_logo,institution_intro')->find();
        if($financeData) return $this->success($financeData->toArray());
         return $this->fail('机构不存在');
    }
}